<?php
/*
	页面类型
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
model('core/core')->checklogin();
$return['setting']=$this->plugin_setting('setting');
if($_RQ['op']=='main'){
	$return['item']=array();
	$return['goods']=pdo_get('jy_exchange_group',array('type'=>1,'uniacid'=>$_SESSION['uniacid'],'endtime >'=>time()));
	if($return['goods']['goods']){
		$return['goods']['goods']=json_decode($return['goods']['goods'],true);
		$return['goods']['price']=0;
		foreach($return['goods']['goods'] as &$g){
			$price=pdo_getcolumn('jy_weishop_goods',array('id'=>$g['goodsid']),'price');
			if($price>$return['goods']['price']){
				$return['goods']['price']=$price;
			}
		}
	}
	$return['balance']=pdo_get('jy_exchange_group',array('type'=>2,'uniacid'=>$_SESSION['uniacid'],'endtime >'=>time()));
	$return['credit']=pdo_get('jy_exchange_group',array('type'=>3,'uniacid'=>$_SESSION['uniacid'],'endtime >'=>time()));
	$return['coupon']=pdo_get('jy_exchange_group',array('type'=>4,'uniacid'=>$_SESSION['uniacid'],'endtime >'=>time()));
	$return['red']=pdo_get('jy_exchange_group',array('type'=>5,'uniacid'=>$_SESSION['uniacid'],'endtime >'=>time()));
	if($_RQ['code']){
		$return['item']['code']=trim($_RQ['code']);
		$code=pdo_get('jy_exchange_code',array('key'=>trim($_RQ['code'])));
		$info=pdo_get('jy_exchange_group',array('id'=>$code['groupid']));
		$return['rule']=$info['rule'];
	}
	$return['pluginset']=$this->plugin_setting('setting');
	$return['isfollow']=pdo_getcolumn('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'follow');	
	if($return['pluginset']['isfollow'] && !$return['isfollow']){
		$return['needfollow']=true;
	}
	set_html('title','兑换中心');
	exi($return);
}

if($_RQ['op']=='post'){
	$beginToday = mktime(0,0,0,date('m'),date('d'),date('Y'));
	$set=$this->plugin_setting('setting');
	if($set['allmax']){
		$count=pdo_count('jy_exchange_record',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'time >='=>$beginToday));
		if($set['allmax']<=$count){
			exi('您今日已达到最大兑换次数，请明天再来');
		}
	}
	$beginToday = mktime(0,0,0,date('m'),date('d'),date('Y'));
	$post=get_postdata();	
	$code=pdo_get('jy_exchange_code',array('key'=>trim($post['code'])));
	if(!$code){
		exi('兑换码错误','error');
	}
	if($code['status']==0){
		exi('兑换码已被使用','error');
	}
	if($code['endtime']<time()){
		exi('兑换码已过期','error');
	}
	$info=pdo_get('jy_exchange_group',array('id'=>$code['groupid']));
	if($info['endtime']<time()){
		exi('兑换活动已结束','error');
	}
	//商品
	if($info['type']==1){
		exi('','error','jy_exchange/goods/main/id='.$code['id']);
	}
	$isadd=pdo_get('jy_exchange_record',array('key'=>$code['key'],'groupid'=>$code['groupid'],'mid'=>$_SESSION['uid'],'status'=>1,'mode'=>$info['type'],'uniacid'=>$_SESSION['uniacid']));
	$order_id=$isadd['id'];
	if(!$order_id){
		pdo_insert('jy_exchange_record',array('key'=>$code['key'],'groupid'=>$code['groupid'],'mid'=>$_SESSION['uid'],'status'=>1,'mode'=>$info['type'],'time'=>time(),'uniacid'=>$_SESSION['uniacid']));	
		$order_id=pdo_insertid();
	}
	if($info['price']>0){
		//支付
		$payid=$isadd['pay_id'];
		if(!$payid){
			$params=array(
				'id'=>$order_id,
				'title'=>'兑换中心订单',
				'fee'=>$info['price'],
				//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
				'no_type'=>array('hdfk','credit3','credit4','credit5','credit6'),//可选，此项作用为指定不允许使用哪种支付方式
				'back_url'=>'jy_exchange/index/payresult/',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
				'success_url'=>'jy_exchange/index/',
				'error_url'=>'jy_exchange/index/',
				'plugin'=>'jy_exchange',
			);
			$payid=cfc('pay')->create_pay_order($params);
			pdo_update('jy_exchange_record',array('pay_id'=>$payid), array('id' => $order_id));
		}
		$return['payid']=$payid;
		$return['id']=$order_id;
		exi($return);
	}else{
		//余额
		if($info['type']==2){
			if($info['balance_type']==1){
				$money=(rand($info['balance_left']*100,$info['balance_right']*100))/100;
			}else{
				$money=$info['balance'];
			}
			model('core/member')->add_credit2($_SESSION['uid'],$money,'兑换码-兑余额');
			pdo_update('jy_exchange_code',array('status'=>0,'balancestatus'=>0),array('id'=>$code['id']));
			pdo_update('jy_exchange_group',array('use +='=>1),array('id'=>$info['id']));
			pdo_update('jy_exchange_record',array('balance'=>$money), array('id' => $order_id));
			$msg='兑换成功,余额已充值到账户';
		}
		//积分
		if($info['type']==3){
			if($info['score_type']==1){
				$money=rand($info['score_left'],$info['score_right']);
			}else{
				$money=$info['score'];
			}
			model('core/member')->add_credit1($_SESSION['uid'],$money,'兑换码-兑积分');
			pdo_update('jy_exchange_code',array('status'=>0,'scorestatus'=>0),array('id'=>$code['id']));
			pdo_update('jy_exchange_record',array('score'=>$money), array('id' => $order_id));
			$msg='兑换成功,积分已充值到账户';
		}
		//优惠券
		if($info['type']==4){		
			$data1=array('mid'=>$_SESSION['uid'],'status'=>0,'uniacid'=>$_SESSION['uniacid'],'couponid'=>$info['coupon'],'getfrom'=>0,'createtime'=>TIMESTAMP);
			$data2=array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'couponid'=>$info['coupon'],'createtime'=>TIMESTAMP);
			pdo_update('jy_weishop_coupon',array('last +='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$info['coupon']));
			pdo_insert('jy_weishop_coupon_getlog',$data2);
			pdo_insert('jy_weishop_coupon_log',$data1);
			pdo_update('jy_exchange_code',array('status'=>0,'couponstatus'=>0),array('id'=>$code['id']));
			$msg='兑换成功,优惠券已发放到账户';
		}
		//红包
		if($info['type']==5){
			if($info['red_type']==1){
				$money=rand($info['red_left'],$info['red_right']);
			}else{
				$money=$info['red'];
			}
			$params=array(
						'mid'=>$_SESSION['uid'],
						'money'=>$money,
						'remark'=>'返现金红包',
			);
			model('core/red_pack')->get_money($params);
			pdo_update('jy_exchange_code',array('status'=>0,'redstatus'=>0),array('id'=>$code['id']));
			pdo_update('jy_exchange_record',array('red'=>$money), array('id' => $order_id));
			$msg='兑换成功,现金红包已到账户';
		}
		pdo_update('jy_exchange_group',array('use +='=>1),array('id'=>$info['id']));
		pdo_update('jy_exchange_record',array('status'=>0), array('id' => $order_id));
		exi($msg);
	}
}
if($_RQ['op']=='payresult'){
		$payresult=$_SESSION['pay_result'];
		if(!$payresult){
			exi('支付失败，未收到支付结果！','error');
		}
		if($payresult['status']!=1){
			exi('支付失败！','error');
		}
		$order=pdo_get('jy_exchange_record',array('id'=>$payresult['id']));
		if(!$order){
			exi('支付失败！订单不存在！','error');
		}
		$info=pdo_get('jy_exchange_group',array('id'=>$order['groupid']));
		$code=pdo_get('jy_exchange_code',array('key'=>$order['key'],'uniacid'=>$_SESSION['uniacid']));
		$update=array(
			'paytype'=>$payresult['pay_type'],
			'transid'=>$payresult['transaction_id'],
			'status'=>0,
			'paytime'=>time(),
		);
		//余额
		if($info['type']==2){
			if($info['balance_type']==1){
				$money=(rand($info['balance_left']*100,$info['balance_right']*100))/100;
			}else{
				$money=$info['balance'];
			}
			model('core/member')->add_credit2($_SESSION['uid'],$money,'兑换码-兑余额');
			pdo_update('jy_exchange_code',array('status'=>0,'balancestatus'=>0),array('id'=>$code['id']));
			pdo_update('jy_exchange_group',array('use +='=>1),array('id'=>$info['id']));
			$update['balance']=$money;
		}
		//积分
		if($info['type']==3){
			if($info['score_type']==1){
				$money=rand($info['score_left'],$info['score_right']);
			}else{
				$money=$info['score'];
			}
			model('core/member')->add_credit1($_SESSION['uid'],$money,'兑换码-兑积分');
			pdo_update('jy_exchange_code',array('status'=>0,'scorestatus'=>0),array('id'=>$code['id']));
			$update['score']=$money;
		}
		//优惠券
		if($info['type']==4){		
			$data1=array('mid'=>$_SESSION['uid'],'status'=>0,'uniacid'=>$_SESSION['uniacid'],'couponid'=>$info['coupon'],'getfrom'=>0,'createtime'=>TIMESTAMP);
			$data2=array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'couponid'=>$info['coupon'],'createtime'=>TIMESTAMP);
			pdo_update('jy_weishop_coupon',array('last +='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$info['coupon']));
			pdo_insert('jy_weishop_coupon_getlog',$data2);
			pdo_insert('jy_weishop_coupon_log',$data1);
			pdo_update('jy_exchange_code',array('status'=>0,'couponstatus'=>0),array('id'=>$code['id']));
		}
		//红包
		if($info['type']==5){
			if($info['red_type']==1){
				$money=rand($info['red_left'],$info['red_right']);
			}else{
				$money=$info['red'];
			}
			$params=array(
						'mid'=>$_SESSION['uid'],
						'money'=>$money,
						'remark'=>'返现金红包',
			);
			model('core/red_pack')->get_money($params);
			pdo_update('jy_exchange_code',array('status'=>0,'redstatus'=>0),array('id'=>$code['id']));
			$update['red']=$money;
		}
		pdo_update('jy_exchange_record',$update, array('id' => $order['id']));
		$order['price']=$info['price'];
		$order['ordersn']='code:'.$order['key'];
		$order['api_data']=array('code'=>$order['key'],'groupid'=>$order['groupid'],'fenfee'=>$info['fenfee']);
		$order['api_data']=ijson_encode($order['api_data']);
		$this->model('model')->create($order);		
		exi('提交成功!');
}